const app = getApp()
Page({
  data: {
    address_list:[],
    show:false,
    choose:false
  },
  onLoad: function (options) {
    this.setData({
      choose:options.choose?true:false
    })
    this.getAddressList()
  },
getAddressList(){
  let _this = this;
  app.util.request({
      url: "api/terminus/findall",
      method: "get",
      success(response) {
          if (response.data.code == 0) {
            console.log(response)
              _this.setData({
                 address_list: response.data.data
              })
          }
      }
  })
},
showDelete(e){
  if(e.detail=="right"){
  this.data.currentId=e.currentTarget.dataset.id
  this.setData({
    show:true
  })
}
},
onClose(e){
  this.setData({
    show:false
  })
},
toDel(){
  let _this = this;
  app.util.request({
      url: "api/terminus/delete",
      method: "post",
      data:{
        id:_this.data.currentId
      },
      success(response) {
          if (response.data.code == 0) {
            wx.showToast({
              title: '删除成功',
            })
            _this.getAddressList()
          } else {
            wx.showToast({
              title: response.data.msg,
              icon: 'none'
            })
          }
      }
  })
},
toEditAddress(e){
  console.log(e)
  if(this.data.choose){
    wx.setStorageSync('addressIndex', e.currentTarget.dataset.index)
    wx.navigateBack({
      delta: 1
      })
  }else{
    wx.navigateTo({
      url: '/pages/address/edit-address/edit-address?index='+e.currentTarget.dataset.index
    })
  }  
},
toAddNewAddress(){
  wx.navigateTo({
    url: '/pages/address/edit-address/edit-address',
  })
},
})